package com.example.work.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class SqlGenerator {
    
    private static final DateTimeFormatter DATE_TIME_FORMATTER = 
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 生成更新指定ID记录的update_time和asset_id的SQL语句（参数化）
     * @param id 记录ID
     * @param assetId 新的资产ID
     * @return 参数化SQL语句
     */
    public static String generateUpdateSql(String id, String assetId) {
        return String.format(
            "UPDATE rank_log_asset_info_basis_web " +
            "SET asset_id = '%s', update_time = NOW() " +
            "WHERE id = '%s';",
            assetId, id
        );
    }
    
    /**
     * 生成更新指定ID记录的update_time和asset_id的SQL语句（带具体时间值）
     * @param id 记录ID
     * @param assetId 新的资产ID
     * @param updateTime 更新时间
     * @return 带具体时间值的SQL语句
     */
    public static String generateUpdateSqlWithTime(String id, String assetId, LocalDateTime updateTime) {
        String formattedTime = updateTime.format(DATE_TIME_FORMATTER);
        return String.format(
            "UPDATE rank_log_asset_info_basis_web " +
            "SET asset_id = '%s', update_time = '%s' " +
            "WHERE id = '%s';",
            assetId, formattedTime, id
        );
    }
    public static String generateUpdateSqlWithTime(String id, String assetId, String updateTime) {
        return String.format(
                "UPDATE rank_log_asset_info_basis_web " +
                        "SET asset_id = '%s', update_time = '%s' " +
                        "WHERE id = '%s';",
                assetId, updateTime, id
        );
    }

    public static String generateUpdateSqlWithTimeWebCard(String id, String assetId, String updateTime) {
        return String.format(
                "UPDATE rank_log_asset_info_system_basis_web_card " +
                        "SET asset_id = '%s', update_time = '%s' " +
                        "WHERE id = '%s';",
                assetId, updateTime, id
        );
    }
    
    /**
     * 生成批量更新SQL语句
     * @param updates 更新信息数组，每个元素包含id和assetId
     * @return 批量更新SQL语句
     */
    public static String generateBatchUpdateSql(AssetUpdate[] updates) {
        StringBuilder sql = new StringBuilder(
            "UPDATE rank_log_asset_info_basis_web " +
            "SET asset_id = CASE id "
        );
        
        for (AssetUpdate update : updates) {
            sql.append(String.format(
                "WHEN '%s' THEN '%s' ",
                update.getId(), update.getAssetId()
            ));
        }
        
        sql.append("END, update_time = NOW() WHERE id IN (");
        
        for (int i = 0; i < updates.length; i++) {
            if (i > 0) sql.append(", ");
            sql.append("'").append(updates[i].getId()).append("'");
        }
        
        sql.append(");");
        return sql.toString();
    }
    
    /**
     * 资产更新信息内部类
     */
    public static class AssetUpdate {
        private String id;
        private String assetId;
        
        public AssetUpdate(String id, String assetId) {
            this.id = id;
            this.assetId = assetId;
        }
        
        public String getId() { return id; }
        public String getAssetId() { return assetId; }
    }
}